/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { checkAndTriggerRefresh } from '@/components/_pages/red-point/_utils';
import request from '@/utils/request';
import { message } from 'antd';
import isArray from 'lodash/isArray';

// 超限模版审核
export async function getWaitForApproveTemplate(params: any, fromRedDot?: boolean) {
  return request('/CustomerService/getWaitForApproveTemplate', {
    method: 'POST',
    data: params,
  })
    .then((res) => {
      const { code, data, msg } = res;
      if (code == 0) {
        const list = Object.keys(data).map((item) => ({
          tid: item,
          content: data[item].content,
          cmPhone: data[item].cmPhone,
        }));
        return { data: list, total: list.length };
      } else {
        message.error(msg);
        return {};
      }
    })
    .then(checkAndTriggerRefresh(getWaitForApproveTemplate, fromRedDot));
}
export async function artificialDealTemplate(params: any) {
  return request('/CustomerService/artificialDealTemplate', {
    method: 'POST',
    data: params,
  });
}

// 添加敏感词
export async function getWorkList(params: any) {
  return request('/SmsSensitiveWords/getList', {
    method: 'POST',
    data: params,
  }).then((res) => {
    const { data } = res;
    if (isArray(data.list) && data.list.length > 0) {
      return { data: data.list, total: data.count };
    } else {
      return {};
    }
  });
}
export async function deteleWork(params: any) {
  return request('/SmsSensitiveWords/deleteSensitiveWord', {
    method: 'POST',
    data: params,
  });
}
export async function addWork(params: any) {
  return request('/SmsSensitiveWords/addSensitiveWord', {
    method: 'POST',
    data: params,
  });
}

//短信模版列表&短信列表
export async function templateQuery(params: any) {
  return request('/CustomerServiceV2/templateQuery', {
    method: 'POST',
    data: params,
  }).then(({ code, data, msg }) => {
    if (code == 0) {
      if (isArray(data.retList) && data.retList.length > 0) {
        return {
          data: data.retList,
          total: data.total_records,
        };
      }
      return {};
    } else {
      message.error(msg);
      return {};
    }
  });
}

export async function setOptions(url: string, params: any) {
  return request(url, {
    method: 'POST',
    data: params,
  });
}
